package com.atguigu.gmall.common.config;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * title: 统一异常处理
 * author: Gyh
 * date:2023-04-17 16:39
 * Description:
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理运行时异常
     * @param e
     * @return
     */
    @ExceptionHandler(RuntimeException.class)
    public Result exceptionError(RuntimeException e) {
        log.error("全局异常RuntimeException捕获:{}", e);
        return Result.fail().code(ResultCodeEnum.FAIL.getCode()).message(e.getMessage());
    }

    /**
     * 处理各种类型异常
     */
    @ExceptionHandler(Exception.class)
    public Result exceptionError(Exception e) {
        log.error("全局异常Exception处理:{}", e);
        return Result.fail().code(ResultCodeEnum.FAIL.getCode()).message(e.getMessage());
    }

}
